//
//  YHVisitorLoginView.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/20.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol YHVisitorLoginViewDelegate: NSObjectProtocol{
    
    optional func userWillLogin()
    func userWillRegister()
}

class YHVisitorLoginView: UIView {
    
    weak var visitorDelegate:YHVisitorLoginViewDelegate?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVisitorInfo(tipText: String, imageName: String, isHome: Bool = false){
        
        if isHome {
           startAnimation()
            
        }else{
            
            bigHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }
        tipLabel.text = tipText
        tipLabel.preferredMaxLayoutWidth = 230
        circleIcon.image = UIImage(named: imageName)
    }
    
    private func startAnimation() {
        
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        
        basic.duration = 15
        
        basic.repeatCount = MAXFLOAT
        
        basic.toValue = 2 * M_PI
        
        basic.removedOnCompletion = false
        
        circleIcon.layer.addAnimation(basic, forKey: nil)
        
        
    }
    private func setupUI() {
        
        addSubview(circleIcon)
        addSubview(coverIcon)
        addSubview(tipLabel)
        addSubview(bigHouse)
        addSubview(loginBtn)
        addSubview(registerBtn)
//        circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        //系统自动布局类型NSLayoutConstraint
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//        //VFL布局
//         tipLabel.translatesAutoresizingMaskIntoConstraints = false
//
//        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[circle]-(16)-[tipLabel]", options: [], metrics: nil, views: ["circle":circleIcon,"tipLabel":tipLabel]))
//        
//        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[tipLabel]-|", options: .AlignAllCenterX, metrics: nil, views: ["circle":circleIcon,"tipLabel":tipLabel]))
        
    circleIcon.snp_makeConstraints { (make) in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY).offset(-80)
        
        }
        
        bigHouse.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.height.equalTo(40)
            
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        coverIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value: CGFloat = 237.0/255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
//YHVisitorLoginView.
        loginBtn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnClick), forControlEvents: .TouchUpInside)
    }
    
    //按钮点击事件
    @objc private func loginBtnClick() {
        
        visitorDelegate?.userWillLogin?()
        
    }
    
    @objc private func registerBtnClick() {
        
        visitorDelegate?.userWillRegister()
        
    }
    private lazy var coverIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var bigHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜", font: 15, textColor: UIColor.darkGrayColor())

    
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)

        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
    
    

}
